<?php 
class Appcloud
{
	public function UpdateRepos()
	{
		$cmd = SVN_CMD." update ".APPCLOUD_REPOS;
		exec($cmd,$output);
		return true;
	}
	
	public function Deploy($instanceConfig)
	{
		$hostname = $instanceConfig['hostname'];
		$domain	  = $instanceConfig['domain'];
		$dbname	  = $instanceConfig['dbname'];
		$dbuser	  = $instanceConfig['dbuser'];
		$dbpass	  = $instanceConfig['dbpasswd'];
		
		//Get Fully qualified domain name 
		$fqdn = strtolower(str_replace(" ", "", $hostname.'.'.$domain));
		
		$siteroot = WEB_ROOT_PATH.DIRECTORY_SEPARATOR.$fqdn;
		$webroot = $siteroot.DIRECTORY_SEPARATOR.WWW_FOLDER;
		$dbfile = $webroot.DIRECTORY_SEPARATOR.'database.sql';
		$deployer = $webroot.DIRECTORY_SEPARATOR.'auto-deployer.php';		
		
		//clear users webroot
		$cmd = RM_CMD." -rf '$webroot'";
		exec($cmd,$output);
		
		//copy files from repos to webroot
		$cmd = CP_CMD." -r '".APPCLOUD_REPOS."' '$webroot'";
		exec($cmd,$output);
		
		//copy deployer to webroot
		$deployer_src = dirname(dirname(__FILE__)).'/deployer/appcloud-auto-deployer.php';
		$cmd = CP_CMD." '".$deployer_src."' '$deployer'";
		exec($cmd,$output);
		
		//setup file file owner
		$cmd = CHOWN_CMD." -R ".FILE_OWNER.":".FILE_GROUP." '$webroot'";
		exec($cmd,$output);
		
		//setup file permission
		$cmd = CHMOD_CMD." -R ".FILE_PERM_ON_WORK." '$webroot/files'";
		exec($cmd,$output);
		
		//setup database connection
		$templateFile = $webroot.DIRECTORY_SEPARATOR.'application.xml';
		$templateData = file_get_contents($templateFile);
		$templateData = str_replace('{$DBNAME}',$dbname,$templateData);
		$templateData = str_replace('{$DBUSER}',$dbuser,$templateData);
		$templateData = str_replace('{$DBPASS}',$dbpass,$templateData);
		file_put_contents($templateFile, $templateData);
		
		
		//load database
		if(is_file($dbfile))
		{
			$cmd = MYSQL_CMD." -u$dbuser -p$dbpass $dbname < '$dbfile'";
			exec($cmd,$output);			
			@unlink($dbfile);
		}
		
		//trigger Auto-Deployer script
		if(is_file($deployer))
		{
			@chmod($deployer,0777);
			$cmd = $deployer;
			exec($cmd,$output);
			@unlink($deployer);
		}	
		
		//setup default constants like URL
		$templateFile = $webroot.DIRECTORY_SEPARATOR.'bin'.DIRECTORY_SEPARATOR.'app_init.php';
		$templateData = file_get_contents($templateFile);
		$value = 'http://'.$fqdn;
        $templateData = preg_replace("/define\([\'\\\"]{1}SITE_URL[\'\\\"]{1}.*?\)\;/i","define('SITE_URL','$value');",$templateData);	            			
		file_put_contents($templateFile, $templateData);
		
		
		//setup file file owner
		$cmd = CHOWN_CMD." -R ".FILE_OWNER.":".FILE_GROUP." '$webroot'";
		exec($cmd,$output);
		
		//setup file permission
		$cmd = CHMOD_CMD." -R ".FILE_PERM_ON_WORK." '$webroot/files'";
		exec($cmd,$output);
		return true;
	}
	
	public function Update($instanceConfig)
	{
		$hostname = $instanceConfig['hostname'];
		$domain	  = $instanceConfig['domain'];
		
		//Get Fully qualified domain name 
		$fqdn = strtolower(str_replace(" ", "", $hostname.'.'.$domain));
		
		$siteroot = WEB_ROOT_PATH.DIRECTORY_SEPARATOR.$fqdn;
		$webroot = $siteroot.DIRECTORY_SEPARATOR.WWW_FOLDER;
		
		//Run SVN update for update files
		$cmd = SVN_CMD." update ".$webroot;
		exec($cmd,$output);
		
		//Trigger Load module action
		
		return true;
	}
	
	public function SetLicnese($instanceConfig,$module,$license_key)
	{
		$hostname = $instanceConfig['hostname'];
		$domain	  = $instanceConfig['domain'];
		
		//Get Fully qualified domain name 
		$fqdn = strtolower(str_replace(" ", "", $hostname.'.'.$domain));
		
		$siteroot = WEB_ROOT_PATH.DIRECTORY_SEPARATOR.$fqdn;
		$webroot = $siteroot.DIRECTORY_SEPARATOR.WWW_FOLDER;
		
		$licensePath = $webroot.DIRECTORY_SEPARATOR.'modules'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.'license.key';
		file_put_contents($licensePath, $license_key);
		return true;
	}
}
?>